export enum State{
    "UNASSIGNED"="UNASSIGNED",
    "INITIALIZING"="INITIALIZING",
    "RELOCATING"="RELOCATING",
    "STARTED"="STARTED",
}


export interface IndexShard{
    index: string;
    shard: string;
    prirep: "p"|"r";
    state: State;
    docs: string;
    store: string;
    ip: string;
    node: string;
}

export type ShardType = {
    docs: number,
    ip: string,
    node: string,
    prirep: "r" | "p",
    shard: number,
    store: string,
    state: State
    unassignedReason: string
}
export type Shard = {
    index: string,
    docs: number,
    types: Array<ShardType>
}

export const createType =(shard: IndexShard):ShardType => {
    return {
        docs: Number(shard.docs),
        ip: shard.ip,
        node: shard.node,
        prirep: shard.prirep,
        shard: Number(shard.shard),
        store: shard.store,
        state: shard.state,
        unassignedReason:shard["unassigned.reason"]
    }
}

export const createShard = (shard: IndexShard):Shard => {
    let docs=0;
    if(shard.prirep==='p'){
        docs=docs+Number(shard.docs);
    }
    return {
        index: shard.index,
        docs: docs,
        types: [createType(shard)]
    }
}

export const addShard = (shard: Shard,item:IndexShard) =>{
    let docs=0;
    if(item.prirep==='p'){
        docs=docs+Number(item.docs);
    }
    shard.docs=shard.docs + docs;
    shard.types.push(createType(item));
    shard.types.sort((a,b)=>a.shard-b.shard)
}
